/** 
*
* @author  jiwf 
* @date 创建时间：2016年11月15日 下午10:20:51 
* @version 1.0 
* @since  
*  
*/
package com.xbstar.web;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.JsonObject;
import com.xbstar.bean.SessionInfo;
import com.xbstar.bean.UserInfo;
import com.xbstar.bean.UserToSessionRela;
import com.xbstar.dao.SessionRepostitory;
import com.xbstar.dao.UserInfoRepository;
import com.xbstar.dao.UserToSessionRelaRepository;
import com.xbstar.tools.Timer;
import com.xbstar.tools.Utils;

import net.sf.json.JSONArray;

@RestController
@RequestMapping("/session")
public class SessionController {
	@Autowired
	SessionRepostitory sessionRe;
	@Autowired
	UserToSessionRelaRepository user2SessionRe;
	@Autowired
	UserInfoRepository  userRe;
	@Autowired
	DataSource dataSource;
	@RequestMapping("/getalluser")
	public String getAllUserBySessionId(@RequestParam("sessionId")Long sessionId){
		JSONObject json=new JSONObject();
		SessionInfo session=sessionRe.findOne(sessionId);
		if(session==null){
			json.put("status", "403");
		}else{
			try {
				Connection conn=dataSource.getConnection();
				String sql="select * from user_to_session_rela where sessionBelongTo=?";
				PreparedStatement ps1 = conn.prepareStatement(sql);
				 ps1.setString(1, session.getSessionBelongTo());
				 ResultSet rs1 = ps1.executeQuery();
				 
				 JSONArray array=new JSONArray();
				 while (rs1.next()) {
					 JSONObject ajson=new JSONObject();
					 ajson.put("headImgUrl", rs1.getString("headImgUrl"));
					 ajson.put("nickName", rs1.getString("nickName"));
					 ajson.put("userId", rs1.getString("userId"));
					 ajson.put("sessionId", session.getId());
					 ajson.put("sessionBelongTo", rs1.getString("sessionBelongTo"));
					 array.add(ajson);
				}
				 json.put("status", "200");
				 json.put("data", array);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return json.toString();
	}
	@RequestMapping("/makesession")
	public String makeAsessionToUserid(@RequestParam("from")Long from,@RequestParam("to")Long to){
		JSONObject json=new JSONObject();
		JSONObject tjson=new JSONObject();
		List<UserToSessionRela> arr=user2SessionRe.findByUserIdAndToUserId(from, to);
		UserInfo toUser=userRe.findOne(to);
		tjson.put("sessionName", toUser.getNickName());
		tjson.put("imgUrl", toUser.getHeadImgUrl());
		if(arr!=null&&arr.size()>0){
			UserToSessionRela uts2=arr.get(0);
			SessionInfo se=sessionRe.findTop1BySessionBelongTo(uts2.getSessionBelongTo());
			if(se==null){
//				String sesssionBleongto=Utils.getUUID();
//				//保存对应关系
//				UserToSessionRela uts=new UserToSessionRela();
//				uts.setJoinTime(Timer.getCurrentString());
//				uts.setUserId(from);
//				uts.setToUserId(to);
//				uts.setSessionBelongTo(sesssionBleongto);
//				user2SessionRe.save(uts);
//				UserToSessionRela uts1=new UserToSessionRela();
//				uts1.setJoinTime(Timer.getCurrentString());
//				uts1.setUserId(to);
//				uts1.setToUserId(from);
//				uts1.setSessionBelongTo(sesssionBleongto);
//				user2SessionRe.save(uts1);
				//新建session
				SessionInfo sess=new SessionInfo();
				sess.setSessionBelongTo(uts2.getSessionBelongTo());
				sess.setLeaderId(to);
				sess=sessionRe.save(sess);
				tjson.put("sessionId", sess.getId());
				tjson.put("sessionBelongTo",uts2.getSessionBelongTo());
			}else{
				tjson.put("sessionId", se.getId());
				tjson.put("sessionBelongTo", uts2.getSessionBelongTo());
			}
		}else{
			String sesssionBleongto=Utils.getUUID();
			//保存对应关系
			UserToSessionRela uts=new UserToSessionRela();
			uts.setJoinTime(Timer.getCurrentString());
			uts.setUserId(from);
			uts.setToUserId(to);
			uts.setSessionBelongTo(sesssionBleongto);
			user2SessionRe.save(uts);
			UserToSessionRela uts1=new UserToSessionRela();
			uts1.setJoinTime(Timer.getCurrentString());
			uts1.setUserId(to);
			uts1.setToUserId(from);
			uts1.setSessionBelongTo(sesssionBleongto);
			user2SessionRe.save(uts1);
			//新建session
			SessionInfo sess=new SessionInfo();
			sess.setSessionBelongTo(sesssionBleongto);
			sess.setLeaderId(to);
			sess=sessionRe.save(sess);
			tjson.put("sessionId", sess.getId());
			tjson.put("sessionBelongTo",sesssionBleongto);
		}
		json.put("data", tjson);
		json.put("status", "200");
		return json.toString();
	}
}
